# -*- coding: UTF-8 -*-
import redis
from logger import LOG


class RedisUtils(object):

    def __init__(self, host, port, pwd=''):
        try:
            # self.client = redis.Redis(host=host, port=port, password=pwd)
            self.pool = redis.ConnectionPool(host=host, port=port, password=pwd, max_connections=300)
            LOG.info("Create redispool: %s:%d]" % (host, port))
        except Exception as e:
            LOG.error("Failed to create redispool: %s:%d ! ERROR: %s" % (host, port, str(e)))

    def get_redis(self):
        try:
            client = redis.StrictRedis(connection_pool=self.pool)
            LOG.info("Create redis client")
        except Exception as e:
            LOG.error("Failed to create redis client ! ERROR: %s" % (str(e)))
        return client

    def llen(self, key):
        return self.client.llen(key)

    def rpush(self, key, item):
        return self.client.rpush(key, item)

    def lpop(self, key):
        return self.client.lpop(key)
